function LaserPathSegment(x, y, angle, length) {
  this.x = x;
  this.y = y;
  this.angle = angle;
  this.length = length;

  LaserPathSegment.prototype.render = function(processing) {
    processing.pushMatrix();
    processing.translate(this.x, this.y);
    processing.rotate(processing.radians(this.angle));
    processing.fill(255, 255, 0);
    processing.quad(this.length, -2,
		    this.length, 2,
		    0, 2,
		    0, -2);
    
    processing.popMatrix();
  }
}

function LaserPath(x, y, angle) {
  this.x = x;
  this.y = y;
  this.angle = angle;
  this.laserSegments = new Array();
  this.isActive = true;
  
  LaserPath.prototype.getInterruptDistance = function(x, y, angle) {
    return -1;
  }

  LaserPath.prototype.doLaserPath = function(x, y, angle, objects, sourceObjectIndex) {
    if (! this.isActive) {
      return;
    }
    var laserOutOfBox = false;
    var minInterruptDistance = -1;
    var minInterruptObject = null;
    var minInterruptI = -1;
    for (var i = 0; i < objects.length; i++) {
      if (sourceObjectIndex == i) {
	continue;
      }
      var interruptDistance = objects[i].getInterruptDistance(x, y, angle);
      if (interruptDistance >= 0) {
	if (minInterruptDistance < 0 || interruptDistance < minInterruptDistance) {
	  minInterruptDistance = interruptDistance;
	  minInterruptObject = objects[i];
	  minInterruptI = i;
	}
      }
    }
    if (minInterruptDistance >= 0) {
      this.laserSegments.push(new LaserPathSegment(x, y, angle, minInterruptDistance));
      if (minInterruptObject.reflectsLaser) {
	var newX = x + minInterruptDistance * Math.cos(Math.PI * angle / 180.0);
	var newY = y + minInterruptDistance * Math.sin(Math.PI * angle / 180.0);
	var newAngle = minInterruptObject.newLaserAngle(x, y, angle);
	this.doLaserPath(newX, newY, newAngle, objects, minInterruptI);
      }
      minInterruptObject.handleLaser(x, y, angle);
    } else {
      this.laserSegments.push(new LaserPathSegment(x, y, angle, Math.max($x, $y)));
    }
  }    

  LaserPath.prototype.update = function(objects) {
    this.laserSegments.length = 0;
    this.doLaserPath(this.x, this.y, this.angle, objects, -1);
    //var distanceToBoxEdge = computeEdgeDistance(this.x, this.y, this.angle);
    //this.laserSegments.push(new LaserPathSegment(this.x, this.y, this.angle, 100));
    // TODO: do math
    // TODO: how to get list of objects up in here
  }

  LaserPath.prototype.render = function(processing) {
    //this.laserSegments.push(new LaserPathSegment(this.x, this.y, this.angle, 100));
    for (var i = 0; i < this.laserSegments.length; i++) {
      this.laserSegments[i].render(processing);
    }
  }
  
  LaserPath.prototype.collides = function() {
		return false;
  }
}
